﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls.Primitives;
using Mengsk.ProxyDelivery.Print.ViewModels;
using System.Windows.Controls;
using System.Windows.Input;
using Mengsk.ProxyDelivery.Domain;
using Mengsk.ProxyDelivery.Print.Domain;


namespace Mengsk.ProxyDelivery.Print.ViewModels
{
    abstract class PrintTemplateItemViewModel : DependencyObject
    {
        public static readonly DependencyProperty XProperty = DependencyProperty.Register("X", typeof(double), typeof(PrintTemplateItemViewModel));
        public static readonly DependencyProperty YProperty = DependencyProperty.Register("Y", typeof(double), typeof(PrintTemplateItemViewModel));
        public static readonly DependencyProperty WidthProperty = DependencyProperty.Register("Width", typeof(double), typeof(PrintTemplateItemViewModel));
        public static readonly DependencyProperty HeightProperty = DependencyProperty.Register("Height", typeof(double), typeof(PrintTemplateItemViewModel));
        public static readonly DependencyProperty FontNameProperty = DependencyProperty.Register("FontName", typeof(string), typeof(PrintTemplateItemViewModel));
        public static readonly DependencyProperty FontSizeProperty = DependencyProperty.Register("FontSize", typeof(double), typeof(PrintTemplateItemViewModel));
        public static readonly DependencyProperty FormatProperty = DependencyProperty.Register("Format", typeof(string), typeof(PrintTemplateItemViewModel));
        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(PrintTemplateItemViewModel));
        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(PrintTemplateItemViewModel));

        public double X { get { return (double)this.GetValue(XProperty); } set { this.SetValue(XProperty, value); } }

        public double Y { get { return (double)this.GetValue(YProperty); } set { this.SetValue(YProperty, value); } }

        public double Width { get { return (double)this.GetValue(WidthProperty); } set { this.SetValue(WidthProperty, value); } }

        public double Height { get { return (double)this.GetValue(HeightProperty); } set { this.SetValue(HeightProperty, value); } }

        public string FontName { get { return (string)this.GetValue(FontNameProperty); } set { this.SetValue(FontNameProperty, value); } }

        public double FontSize { get { return (double)this.GetValue(FontSizeProperty); } set { this.SetValue(FontSizeProperty, value); } }

        public string Format { get { return (string)this.GetValue(FormatProperty); } set { this.SetValue(FormatProperty, value); } }

        public string Title { get { return (string)this.GetValue(TitleProperty); } set { this.SetValue(TitleProperty, value); } }

        public string Value { get { return (string)this.GetValue(ValueProperty); } set { this.SetValue(ValueProperty, value); } }

        public Thumb UI { get; protected set; }

        public PrintTemplateItem Data { get; protected set; }

        public abstract FrameworkElement PropertyUI { get; protected set; }

        public event MouseEventHandler PreviewMouseLeftButtonDown;

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            if (e.Property == XProperty)
            {
                Canvas.SetLeft(this.UI, double.Parse(e.NewValue.ToString()));
                this.Data.X = double.Parse(e.NewValue.ToString());
            }
            else if (e.Property == YProperty)
            {
                Canvas.SetTop(this.UI, double.Parse(e.NewValue.ToString()));
                this.Data.Y = double.Parse(e.NewValue.ToString());
            }
            else if (e.Property == WidthProperty)
            {
                this.UI.Width = double.Parse(e.NewValue.ToString());
                this.Data.Width = double.Parse(e.NewValue.ToString());
            }
            else if (e.Property == HeightProperty)
            {
                this.UI.Height = double.Parse(e.NewValue.ToString());
                this.Data.Height = double.Parse(e.NewValue.ToString());
            }
            else if (e.Property == FontSizeProperty)
            {
                this.UI.FontSize = double.Parse(e.NewValue.ToString());
                this.Data.FontSize = double.Parse(e.NewValue.ToString());
            }
            else if (e.Property == FontNameProperty)
            {
                this.UI.FontFamily = new System.Windows.Media.FontFamily((string)e.NewValue);
                this.Data.FontName = (string)e.NewValue;
            }
            else if (e.Property == FormatProperty)
            {
                if(this.Data!=null)
                {
                    this.Data.Format = e.NewValue.ToString();
                }
            }
            else if (e.Property == TitleProperty)
            {
                this.Data.Title = e.NewValue.ToString();
            }
            else if (e.Property == ValueProperty)
            {
                //do noting
            }
            else
            {
                MessageBox.Show("未知的属性:" + e.Property.Name);
            }
        }

        protected virtual void OnMouseEnter(MouseEventArgs e)
        {
            if (this.PreviewMouseLeftButtonDown != null)
            {
                this.PreviewMouseLeftButtonDown(this, e);
            }
        }

        private void AttachThumbEvents(Thumb thumb)
        {
            thumb.DragDelta += thumb_DragDelta;
            thumb.SizeChanged += thumb_SizeChanged;
            thumb.PreviewMouseLeftButtonDown += thumb_PreviewMouseLeftButtonDown;
            thumb.PreviewKeyUp += new KeyEventHandler(thumb_PreviewKeyUp);
        }

        void thumb_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.OnMouseEnter(null);
        }


        private void DeattachThumbEvents(Thumb thumb)
        {
            thumb.DragDelta -= thumb_DragDelta;
            thumb.SizeChanged -= thumb_SizeChanged;
            thumb.PreviewMouseLeftButtonDown -= thumb_PreviewMouseLeftButtonDown;
            thumb.PreviewKeyUp -= new KeyEventHandler(thumb_PreviewKeyUp);
        }

        void thumb_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
        {
            Thumb thumb = sender as Thumb;
            var item = thumb.DataContext as PrintTemplateItemViewModel;
            item.Width = (int)thumb.Width;
            item.Height = (int)thumb.Height;
        }

        void thumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            Thumb thumb = sender as Thumb;
            var item = thumb.DataContext as PrintTemplateItemViewModel;
            Canvas.SetLeft(thumb, Canvas.GetLeft(thumb) + e.HorizontalChange);
            Canvas.SetTop(thumb, Canvas.GetTop(thumb) + e.VerticalChange);
            item.X = Canvas.GetLeft(thumb);
            item.Y = Canvas.GetTop(thumb);
        }

        void thumb_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            e.Handled = true;
            Thumb thumb = sender as Thumb;
            if (e.Key == Key.Up)
            {
                Canvas.SetTop(thumb, Canvas.GetTop(thumb) - 1);
            }
            else if (e.Key == Key.Down)
            {
                Canvas.SetTop(thumb, Canvas.GetTop(thumb) + 1);
            }
            else if (e.Key == Key.Left)
            {
                Canvas.SetLeft(thumb, Canvas.GetLeft(thumb) - 1);
            }
            else if (e.Key == Key.Right)
            {
                Canvas.SetLeft(thumb, Canvas.GetLeft(thumb) + 1);
            }
            else
            {
                e.Handled = false;
            }
        }

        public void GenerateFrom(System.Windows.Style uiStyle, PrintTemplateItem item)
        {
            //创建UI
            this.UI = new Thumb { DataContext = this };
            this.UI.Style = uiStyle;
            Data = item;
            this.X = this.Data.X;
            this.Y = this.Data.Y;
            this.Width = this.Data.Width;
            this.Height = this.Data.Height;
            this.FontName = this.Data.FontName;
            this.FontSize = this.Data.FontSize;
            this.Format = this.Data.Format;
            this.Title = this.Data.Title;
            AttachThumbEvents(this.UI);
        }
    }
}
